package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentTechXbzMapper;
import com.zhijian.medical.entity.po.AssessmentTechXbz;
import com.zhijian.medical.entity.vo.request.AssessmentTechXbzSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.AssessmentTechXbzSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentTechXbzVo;
import com.zhijian.medical.service.AssessmentTechXbzService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 技术组技术人员考核-细胞组业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 15:34:35
 */
@Slf4j
@Service
public class AssessmentTechXbzServiceImpl implements AssessmentTechXbzService {

	@Autowired
    private AssessmentTechXbzMapper mapper;

    @Override
    public AssessmentTechXbz selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentTechXbzVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(AssessmentTechXbz assessmentTechXbz) {
        Date now = new Date();
        assessmentTechXbz.setCreateTime(now);
        assessmentTechXbz.setUpdateTime(now);
        return mapper.insert(assessmentTechXbz) > 0 ? assessmentTechXbz.getId() : 0L;
    }

    @Override
    public Long insertSelective(AssessmentTechXbz assessmentTechXbz) {
        Date now = new Date();
        assessmentTechXbz.setCreateTime(now);
        assessmentTechXbz.setUpdateTime(now);
        return mapper.insertSelective(assessmentTechXbz) > 0 ? assessmentTechXbz.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentTechXbz assessmentTechXbz) {
        Date now = new Date();
        assessmentTechXbz.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentTechXbz) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentTechXbz record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(AssessmentTechXbzSaveOrUpdateVo saveOrUpdateVo) {
        AssessmentTechXbz assessmentTechXbz = new AssessmentTechXbz();
        BeanUtils.copyProperties(saveOrUpdateVo, assessmentTechXbz);
        return this.insert(assessmentTechXbz) != 0;
    }

    @Override
    public boolean modify(AssessmentTechXbzSaveOrUpdateVo saveOrUpdateVo) {
        AssessmentTechXbz old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        AssessmentTechXbz assessmentTechXbz = new AssessmentTechXbz();
        BeanUtils.copyProperties(saveOrUpdateVo, assessmentTechXbz);
        return this.update(assessmentTechXbz);
    }

    @Override
    public AssessmentTechXbzVo selectOne(AssessmentTechXbzSearchVo assessmentTechXbzSearchVo) {
        return mapper.selectOne(assessmentTechXbzSearchVo);
     }

    @Override
    public Integer count(AssessmentTechXbzSearchVo assessmentTechXbzSearchVo) {
        return mapper.count(assessmentTechXbzSearchVo);
    }

    @Override
    public List<AssessmentTechXbzVo> selectList(AssessmentTechXbzSearchVo assessmentTechXbzSearchVo) {
        return mapper.selectList(assessmentTechXbzSearchVo);
    }

    @Override
    public Page<AssessmentTechXbzVo> selectPage(AssessmentTechXbzSearchVo assessmentTechXbzSearchVo) {
        AssertUtil.check(Objects.isNull(assessmentTechXbzSearchVo.getPageNum()) || Objects.isNull(assessmentTechXbzSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(assessmentTechXbzSearchVo);
        //分页
        Page<AssessmentTechXbzVo> page = new Page<>(true, count, assessmentTechXbzSearchVo.getPageNum(), assessmentTechXbzSearchVo.getPageSize());
        assessmentTechXbzSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentTechXbzVo> responseVos = this.selectList(assessmentTechXbzSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentTechXbz> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(AssessmentTechXbzSearchVo assessmentTechXbzSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(assessmentTechXbzSearchVo);
    }
    
}